package com.witmed.home.view.resp

import android.content.Context
import com.witmed.lib_common.room.VideoDataBase
import com.witmed.lib_common.room.VideoHistory
import com.witmed.lib_http.ResponseData
import com.witmed.lib_http.RetrofitManager
import com.witmed.lib_http.entity.VideoList
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.withContext

class VideoRepository {

    val apiService by lazy { RetrofitManager.getApiService() }

    /*suspend fun videoList(page:Int,pagesize:Int):ResponseData<VideoList>{
        return withContext(Dispatchers.IO){
            apiService.videoList(page, pagesize)
        }
    }*/

    suspend fun videoList(page:Int,pagesize:Int)=flow<ResponseData<VideoList>>{
        emit(apiService.videoList(page, pagesize))
    }.flowOn(Dispatchers.IO)

    suspend fun insetVideo(videoHistory: VideoHistory,context: Context){
        withContext(Dispatchers.IO){
            VideoDataBase.getDataBase(context).videoDao().insert(videoHistory)
        }
    }
}